package jehc.djshi.oauth.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jehc.djshi.common.annotation.Auth;
import jehc.djshi.common.base.BaseAction;
import jehc.djshi.common.base.BaseResult;
import jehc.djshi.common.util.StringUtil;
import jehc.djshi.oauth.form.OauthAccountTenantForm;
import jehc.djshi.oauth.model.OauthAccountTenant;
import jehc.djshi.oauth.param.OauthAccountTenantParam;
import jehc.djshi.oauth.service.OauthAccountTenantService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Desc 授权中心账户
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/oauthAccountTenant")
@Api(value = "账号租户",tags = "账号租户",description = "账号租户")
public class OauthAccountTenantController extends BaseAction {

    @Resource
    private OauthAccountTenantService oauthAccountTenantService;

    /**
     * 查询账户租户集合
     * @param oauthAccountTenantParam
     */
    @ApiOperation(value="查询账户租户集合", notes="查询账户租户集合")
    @GetMapping(value="/list")
    @Auth(value = "/oauthAccountTenant/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<List<OauthAccountTenant>> getOauthAccountTenantList(OauthAccountTenantParam oauthAccountTenantParam){
        List<OauthAccountTenant> oauthAccountTenantList = oauthAccountTenantService.getOauthAccountTenantList(oauthAccountTenantParam);
        return BaseResult.success(oauthAccountTenantList);
    }

    /**
     * 绑定租户
     * @param oauthAccountTenantForm
     */
    @ApiOperation(value="设定账户租户", notes="设定账户租户")
    @PostMapping(value="/add")
    @Auth(value = "/oauthAccountTenant/add")
    public BaseResult addOauthAccountTenant(@RequestBody OauthAccountTenantForm oauthAccountTenantForm){
        int i = 0;
        if(null != oauthAccountTenantForm){
            i=oauthAccountTenantService.addOauthAccountTenant(oauthAccountTenantForm.getOauthAccountTenants(),oauthAccountTenantForm.getAccount());
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 解除绑定租户
     * @param id
     */
    @DeleteMapping(value="/delete")
    @ApiOperation(value="删除租户", notes="删除租户")
    public BaseResult delOauthAccountTenant(String id){
        int i = 0;
        if(!StringUtil.isEmpty(id)){
            Map<String, Object> condition = new HashMap<String, Object>();
            condition.put("id",id.split(","));
            i=oauthAccountTenantService.delOauthAccountTenant(condition);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }
}
